package content.views.works;
import feffects.easing.Quint;
import haxe.Timer;
import js.Lib;
import tools.ASprite;
import tools.BoundProvider;

/**
 * Nav Works
 * @author PRASS
 */

class NavWorks extends ASprite 
{
	
	private var _items 		: Array<NavItemWorks>;
	private var _timer		: Timer;
	
	private var _current	: NavItemWorks;
	private var _old		: NavItemWorks;
	
	private var i : Int;
	
	public function new(pDom : JQuery) 
	{
		super(pDom);
				
		_items = new Array<NavItemWorks>();
	}
	
	public function build( data : Array<Object> ) : Void
	{
		i = 0;
		var prod : NavItemWorks;
		
		while ( i < data.length )
		{		
			//var html : String = '<div id="p'+i+'" class="prod" ><div id="'+i+'" class="prodRoll"></div><img src="'+data[i].thumb+'" /></div>';
			var html : String = '<div id="p'+i+'" class="prod" ><div id="'+i+'" class="prodRoll"></div></div>';
			dom.append(html);
			
			prod = new NavItemWorks( new JQuery("#p" + i), i);
			prod.dom.css("background-image", "url(" + data[i].thumb + ")");
			_items[i] = prod;
			
			i++;
		}
		
		//setX(-100);
		//setAlpha(0);
	}
	
	public function display() : Void
	{
		//tweens( [ {prop:setX, init:-100, value:0 }, {prop:setAlpha, init:0, value:1 } ], 900, Quint.easeOut, _active);
		
		i = -1;
		
		var limit : Int = _items.length - 1;
		
		while ( i++ < limit )
		{	
			_items[i].tweens( [ { prop:_items[i].setX, init: -100, value:0 }, { prop:_items[i].setAlpha, init:0, value:1 } ], 900, Quint.easeOut, null, i * 50);
		}
		
		_items[limit].tweens( [ {prop:_items[limit].setX, init:-100, value:0 }, {prop:_items[limit].setAlpha, init:0, value:1 } ], 900, Quint.easeOut, _active,limit*50);
	}
	
	public function handleSelect( id : Int ) : Void
	{
		if (_old != null) _old.unselect();
		
		_current = _items[id];
		_current.select();
		
		_old = _current;
	}
	
	private function _active() : Void
	{		
		dom.mousemove( function(e) { checkMousePos(e); } );
		
		if (_timer != null)
		{
			_timer.stop();
			_timer = null;
		}
		
		_mouseY = 0;
		
		_timer = new Timer(25);
		_timer.run = _onMove;
	}
	
	public function hide() : Void
	{
		dom.mousemove( null );
		
		if (_timer != null)
		{
			_timer.stop();
			_timer = null;
		}
		
		i = 0;
		
		while ( i < _items.length )
		{		
			//_items[i].tweens( [ {prop:_items[i].setX, init:0, value:-100 }, {prop:_items[i].setAlpha, init:1, value:0 } ], 400, Quint.easeOut, null,i*2);
			_items[i].tween( 1, _items[i].setAlpha,0 , 300, Quint.easeOut);
			i++;
		}
	}

	
	private function checkMousePos(e) : Void
	{
		_mouseY = e.pageY;
	}
	
	private function _onMove() : Void
	{	
		_ratio = _mouseY/ _h;
		_pos = _ratio * _max;
		
		_smoothPos = getY() + ( _pos - getY() ) * .05;
		
		setY( cast _smoothPos  );
	}
	
	public function resize(w : Int , h : Int ) : Void
	{
		setHeight(h);
		_h = h;
		
		_max = _h - (_items.length * 101);
	}
	
	private var _smoothPos	: Float;
	private var _ratio		: Float;
	private var _pos		: Float;
	private var _h			: Int;
	private var _max		: Int;
	private var _mouseY 	: Float;
	
	
}